//
//  UILockView.m
//  手势解锁
//
//  Created by 博广 on 16/8/27.
//  Copyright © 2016年 博广. All rights reserved.
//

#import "UILockView.h"

@interface UILockView ()

/**
 *  所有选中的按钮
 */
@property(nonatomic,strong) NSMutableArray *selectedBtns;
/**
 *  当前点
 */
@property(nonatomic,assign) CGPoint currentPoint;

@end

@implementation UILockView

- (NSMutableArray *)selectedBtns
{
    if (_selectedBtns == nil) {
        _selectedBtns = [NSMutableArray array];
    }
    
    return _selectedBtns;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpButton];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUpButton];
    }
    return self;
}

- (void) setUpButton
{
    for (int i=0; i<9; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        button.tag = i;
        //设置不能点击
        button.userInteractionEnabled = NO;
        [self addSubview:button];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat butW = 74;
    CGFloat butH = 74;
    CGFloat margin = (self.frame.size.width - butW * 3) / 4;
    
    for (int i = 0; i<self.subviews.count; i++) {
        int row = i / 3;
        int col = i % 3;
        CGFloat startX = margin + (butW + margin) * col;
        CGFloat startY = margin + (butH + margin) * row;
        self.subviews[i].frame = CGRectMake(startX, startY, butW, butH);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint downPoint = [self getCurrentPoint:touches];
    
    UIButton *seletedBtn = [self getSelectedButtonWithPoint:downPoint];
    if (seletedBtn) {
        [seletedBtn setSelected:YES];
        [self.selectedBtns addObject:seletedBtn];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint downPoint = [self getCurrentPoint:touches];
    
    UIButton *seletedBtn = [self getSelectedButtonWithPoint:downPoint];
    if (seletedBtn && seletedBtn.selected != YES) {
        [seletedBtn setSelected:YES];
        [self.selectedBtns addObject:seletedBtn];
    }
    
    self.currentPoint = downPoint;
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (UIButton *btn in self.selectedBtns) {
        [btn setSelected:NO];
    }
    
    if ([self.delegate respondsToSelector:@selector(lockView:didFinishWithResult:)]) {
        NSMutableString *result = [NSMutableString string];
        for (UIButton *btn in self.selectedBtns) {
            [result appendFormat:@"%ld",(long)btn.tag];
        }
        NSLog(@"reslut=%@",result);
    }
    
    [self.selectedBtns removeAllObjects];
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextClearRect(context, rect);
    
    [[UIColor whiteColor] set];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineWidth(context, 5);
    
    for (int i = 0; i < self.selectedBtns.count; i++) {
        UIButton *button = self.selectedBtns[i];
        if (0 == i) {
            CGContextMoveToPoint(context, button.center.x, button.center.y);
        } else {
            CGContextAddLineToPoint(context, button.center.x, button.center.y);
        }
    }
    
    if (self.selectedBtns.count != 0) {
        CGContextAddLineToPoint(context, self.currentPoint.x, self.currentPoint.y);
    }
    
    CGContextStrokePath(context);
}

/**
 *  获取当前手指所在的点
 */
- (CGPoint) getCurrentPoint:(NSSet<UITouch *> *) touches
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];
    return point;
}

/**
 *  获取当前选中的按钮
 */
- (UIButton *) getSelectedButtonWithPoint:(CGPoint) point
{
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame, point)) {
            return btn;
        }
    }
    
    return nil;
}
@end
